﻿Imports FairShop.Objects
Imports System.IO
Imports System.ComponentModel

Public Class VerkoopsoverzichtForm

    Private Const TEXT_TOTAALRIJ As String = "Totaal"

    Private _mainForm As Form

#Region "Constructors"

    Public Sub New(ByVal mainForm As Form)
        Me.InitializeComponent()
        _mainForm = mainForm
        InitGrid()
    End Sub

#End Region

#Region "Events"

    Private Sub MaakRapportButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles MaakRapportButton.Click
        MaakRapport(DateTimePickerBeginDatum.Value, DateTimePickerEindDatum.Value)
    End Sub

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If (e.ColumnIndex = Me.LeverancierColumn.Index) Then
            If Not e.Value Is DBNull.Value AndAlso e.Value IsNot Nothing Then
                If e.Value.ToString = TEXT_TOTAALRIJ Then
                    Dim style As New DataGridViewCellStyle()
                    style.Font = New Font(DataGridView1.Font.FontFamily, DataGridView1.Font.Size, FontStyle.Bold)
                    e.CellStyle.ApplyStyle(style)
                End If
            End If
        End If
    End Sub

#End Region

#Region "Helpers"

    Private Sub InitGrid()
        With Me.DataGridView1
            .AutoGenerateColumns = False
        End With
        With Me.LeverancierColumn
            .DataPropertyName = "Leverancier"
            .HeaderText = "Leverancier"
        End With
        With Me.TotaalColumn
            .DataPropertyName = "Totaal"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .DefaultCellStyle.Format = "c"
            .HeaderText = "Totaal"
        End With
    End Sub

    Private Sub MaakRapport(ByVal begindatum As Date, ByVal einddatum As Date)
        Dim verkopenVanPeriode As List(Of VerkoopsoverzichtItem) = (From v In Utility.Data.VerkoopsDetails _
                                                                    Where begindatum <= v.VerkopenRow.Datum _
                                                                    And v.VerkopenRow.Datum <= einddatum _
                                                                    Group By Key = v.ArtikelsRow.LeveranciersRow.Naam Into Group _
                                                                    Order By Key _
                                                                    Select New VerkoopsoverzichtItem _
                                                                    With {.Leverancier = Key, _
                                                                          .Totaal = Group.Sum(Function(v) v.Totaal)}) _
                                                                    .ToList()

        Dim eindtotaal As Decimal = (From v In verkopenVanPeriode _
                                     Select v.Totaal).Sum()
        verkopenVanPeriode.Add(New VerkoopsoverzichtItem With {.Leverancier = TEXT_TOTAALRIJ, .Totaal = eindtotaal})
        DataGridView1.DataSource = verkopenVanPeriode
    End Sub

#End Region

    Private Class VerkoopsoverzichtItem

        Private _leverancier As String
        Private _totaal As Decimal

        Public Property Leverancier() As String
            Get
                Return _leverancier
            End Get
            Set(ByVal value As String)
                _leverancier = value
            End Set
        End Property

        Public Property Totaal() As Decimal
            Get
                Return _totaal
            End Get
            Set(ByVal value As Decimal)
                _totaal = value
            End Set
        End Property

    End Class

End Class